const express = require("express");
const router = express.Router();
const process = require("child_process");
const config = require('../../../config/config.default');//保存全局变量
//路由：http://localhost:3000/api/android/command/
//安卓回到桌面：放在server.use(router)之前：否则就会404
router.get("/command", (req, res) => {
    const detail=req.query['detail'];
    if(!detail){
      detail=`adb connect ${config.androidIP}` 
    }
    process.exec(
      detail,
      (error, stdout, stderr) => {
        if (!error) {
          // 成功
          res.jsonp("s:" + stdout);
        } else {
          // 失败
          res.jsonp("e:" + error);
        }
      }
    );
  });
//路由：http://localhost:3000/api/android/desk
//安卓回到桌面：放在server.use(router)之前：否则就会404
router.get("/desk", (req, res) => {
    // res.jsonp({name:'test测试'})
    // return
    process.exec(
      "C:\\platform-tools\\adb shell input keyevent 3",
      (error, stdout, stderr) => {
        if (!error) {
          // 成功
          res.jsonp("s:" + stdout);
        } else {
          // 失败
          res.jsonp("e:" + error);
        }
      }
    );
  });
  
  //启动安卓:http://localhost:3000/api/android/launch/com.crh2a
  router.get("/launch/:package", (req, res) => {
    const package=req.params['package']
    let activity=req.query['activity']
    if(!activity){
      activity='/.MainActivity'
    }
    console.log(package+activity);
    process.exec(
      `C:\\platform-tools\\adb shell am start -W -n ${package+activity}`,
      (error, stdout, stderr) => {
        if (!error) {
          // 成功
          res.jsonp("s:" + stdout);
        } else {
          // 失败
          res.jsonp("e:" + error);
        }
      }
    );
  });

module.exports = router;
